package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.model.CoordParmFileHandle;
import com.nhc.model.TransParaSeven;
import com.nhc.model.TransParaSevenFileHandle;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.TopMenuWindow;

public class CoordParmActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "CoordParmActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    static final String INTENT_COORDPARM_MODE = "INTENT_COORDPARM_MODE";
    static final String INTENT_COORDPARM_INFO = "INTENT_COORDPARM_INFO";

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */

    public static Intent createIntentOld(Context context, TransParaSeven setInfo) {
        return new Intent(context, CoordParmActivity.class)
                .putExtra(INTENT_COORDPARM_INFO, setInfo);
    }

    public static Intent createIntent(Context context, int mode) {
        return new Intent(context, CoordParmActivity.class)
                .putExtra(INTENT_COORDPARM_MODE, mode);
    }


    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    TransParaSeven _setInfo;
    int _mode = 0;
    boolean _isChange = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coordparmset_activity);

        intent = getIntent();
        _mode = intent.getIntExtra(INTENT_COORDPARM_MODE, _mode);
        if (_mode < 0 || _mode >= SysUtils.COORD_0.length) {
            _mode = 0;
        }
        _setInfo = TransParaSevenFileHandle.getInstance().getListTransParaSeven(_mode);

        tvBaseTitle.setText(SysUtils.COORD_0[_mode] + getString(R.string.parmsetting));
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_DX;
    private TextView tv_DY;
    private TextView tv_DZ;
    private TextView tv_DA;
    private TextView tv_DF;


    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);

        tv_DX = (TextView) findViewById(R.id.tv_DX);
        tv_DY = (TextView) findViewById(R.id.tv_DY);
        tv_DZ = (TextView) findViewById(R.id.tv_DZ);
        tv_DA = (TextView) findViewById(R.id.tv_DA);
        tv_DF = (TextView) findViewById(R.id.tv_DF);

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void initData() {//必须调用
        refresh();
    }

    void refresh() {
        tv_DX.setText(_setInfo.DX + "");
        tv_DY.setText(_setInfo.DY + "");
        tv_DZ.setText(_setInfo.DZ + "");
        tv_DA.setText(_setInfo.getDA() + "");
        tv_DF.setText(_setInfo.getDF() + "");

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.coordparm_0).setOnClickListener(this);
        findViewById(R.id.coordparm_1).setOnClickListener(this);
        findViewById(R.id.coordparm_2).setOnClickListener(this);
        findViewById(R.id.coordparm_3).setOnClickListener(this);
        findViewById(R.id.coordparm_4).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        if (_isChange) {
            CoordParmFileHandle.getInstance().save(this);
            setResult(RESULT_OK);
        }
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onForwardClick(View v) {
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)),
                REQUEST_TO_TOP_MENU, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.coordparm_0:
                editParm(0);
                break;
            case R.id.coordparm_1:
                editParm(1);
                break;
            case R.id.coordparm_2:
                editParm(2);
                break;
            case R.id.coordparm_3:
                editParm(3);
                break;
            case R.id.coordparm_4:
                editParm(4);
                break;
            default:
                break;
        }
    }

    private void editParm(int pos) {
        String strT = "DX";
        int REQUEST = REQUEST_TO_DX;
        double value = _setInfo.DX;
        switch (pos) {
            default:
            case 0:
                break;
            case 1:
                strT = "DY";
                value = _setInfo.DY;
                REQUEST = REQUEST_TO_DY;
                break;
            case 2:
                strT = "DZ";
                value = _setInfo.DZ;
                REQUEST = REQUEST_TO_DZ;
                break;
            case 3:
                strT = "DA";
                value = _setInfo.getDA();
                REQUEST = REQUEST_TO_DA;
                break;
            case 4:
                strT = "DF";
                value = _setInfo.getDF();
                REQUEST = REQUEST_TO_DF;
                break;
        }

        intent = EditNumberActivity.createIntentD(context
                , strT + getString(R.string.parmsetting), value, "");
        toActivity(intent, REQUEST);
    }


    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_DX = 31;
    private final int REQUEST_TO_DY = 32;
    private final int REQUEST_TO_DZ = 33;
    private final int REQUEST_TO_DA = 34;
    private final int REQUEST_TO_DF = 35;


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_DX:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);

                    _setInfo.DX = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_DY:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    _setInfo.DY = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_DZ:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    _setInfo.DZ = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_DA:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    _setInfo.setDA(Double.valueOf(editedValue));
                    refresh();
                }
                break;
            case REQUEST_TO_DF:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    _setInfo.setDF(Double.valueOf(editedValue));
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            _setInfo.reset();
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }

        _isChange = true;
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
